package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.EUDataGridResult;
import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.service.ContentService;
import com.taotao.utils.HttpClientUtil;
@Service
public class ContentServiceImpl implements ContentService {
@Autowired
private TbContentMapper tbContentMapper;
@Value("${REST_BASE_URL}")
private String REST_BASE_URL;
@Value("${REST_CONTENT_SYNC_URL}")
private String REST_CONTENT_SYNC_URL;
	@Override
	public EUDataGridResult getContentlist(Integer page, Integer rows, long categoryId) {
		EUDataGridResult result=new EUDataGridResult();
		TbContentExample example =new TbContentExample();
		if(categoryId!=0){
			example.createCriteria().andCategoryIdEqualTo(categoryId);
		}
		PageHelper.startPage(page, rows);
		List<TbContent> list=tbContentMapper.selectByExample(example);
		result.setRows(list);
		PageInfo<TbContent> pageInfo = new PageInfo<TbContent>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	@Override
	public TaotaoResult insertContent(TbContent tbContent) {
		tbContent.setCreated(new Date());
		tbContent.setUpdated(new Date());
		tbContentMapper.insert(tbContent);
		try{
			HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_SYNC_URL+tbContent.getCategoryId());
		}catch(Exception e){
			e.printStackTrace();
		}
		return TaotaoResult.ok();
	}

}
